package transaction.mq.order.mq;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import transaction.mq.order.config.RabbitMQConfig;
import transaction.mq.order.model.XcTask;
import transaction.mq.order.service.TaskService;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author Administrator
 * @version 1.0
 **/
@Component
@Slf4j
public class ChooseCourseTask {

    @Autowired
    TaskService taskService;

    @RabbitListener(queues = RabbitMQConfig.XC_LEARNING_FINISHADDCHOOSECOURSE)
    public void receiveFinishChooseCourseTask(XcTask xcTask) {
        if (xcTask != null && StringUtils.isNotEmpty(xcTask.getId())) {
            taskService.finishTask(xcTask.getId());
        }
    }

    @Scheduled(cron = "0/5 * * * * *")
    //定时发送加选课任务
    public void sendChooseCourseTask() {
        //得到1分钟之前的时间
        TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
        Calendar calendar = Calendar.getInstance();
        Date date = calendar.getTime();
        log.info("====================" + new SimpleDateFormat("yyyyMMdd HH:mm:ss").format(date));
//        calendar.set(GregorianCalendar.MINUTE, -1);
        calendar.add(GregorianCalendar.SECOND, -20);
        Date time = calendar.getTime();
        log.info("====================" + new SimpleDateFormat("yyyyMMdd HH:mm:ss").format(time));
//        DateTime newDate3 = DateUtil.offsetHour(date, -3);
        List<XcTask> xcTaskList = taskService.findXcTaskList(time, 100);
        System.out.println(xcTaskList);
        //调用service发布消息，将添加选课的任务发送给mq
        for (XcTask xcTask : xcTaskList) {
            //取任务
            if (taskService.getTask(xcTask.getId(), xcTask.getVersion()) > 0) {
                //要发送的交换机
                String ex = xcTask.getMqExchange();
                //发送消息要带routingKey
                String routingKey = xcTask.getMqRoutingkey();
                taskService.publish(xcTask, ex, routingKey);
            }

        }
    }

    //定义任务调试策略
//    @Scheduled(cron="0/3 * * * * *")//每隔3秒去执行
//       @Scheduled(fixedRate = 3000) //在任务开始后3秒执行下一次调度
//       @Scheduled(fixedDelay = 3000) //在任务结束后3秒后才开始执行
    public void task1() {
        log.info("===============测试定时任务1开始===============");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("===============测试定时任务1结束===============");

    }

    //定义任务调试策略
//    @Scheduled(cron="0/3 * * * * *")//每隔3秒去执行
//    @Scheduled(fixedRate = 3000) //在任务开始后3秒执行下一次调度
//       @Scheduled(fixedDelay = 3000) //在任务结束后3秒后才开始执行
    public void task2() {
        log.info("===============测试定时任务2开始===============");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("===============测试定时任务2结束===============");

    }
}
